package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.OutSideTaskAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.OutSideBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;

public class OutSideTaskActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout re_back;
    private XRecyclerView recyclerview_out_side_task;
    private OutSideTaskAdapter outSideTaskAdapter;
    private RelativeLayout re_pending;  //待处理任务
    private TextView tv_pending;
    private TextView tv_line_pending;
    private RelativeLayout re_processed;  //已处理任务
    private TextView tv_processed;
    private TextView tv_line_processed;
    @Override
    public int intiLayout() {
        return R.layout.activity_out_side_task;
    }

    @Override
    public void initView() {
        re_back=findViewById(R.id.re_back);
        re_pending=findViewById(R.id.re_pending);
        tv_pending=findViewById(R.id.tv_pending);
        tv_line_pending=findViewById(R.id.tv_line_pending);
        re_processed=findViewById(R.id.re_processed);
        tv_processed=findViewById(R.id.tv_processed);
        tv_line_processed=findViewById(R.id.tv_line_processed);
        re_pending.setOnClickListener(this);
        re_back.setOnClickListener(this);
        re_processed.setOnClickListener(this);
        recyclerview_out_side_task=findViewById(R.id.recyclerview_out_side_task);
        getNetWork(1);
    }

    private void getNetWork(int rwzt) {
        int ysid= (int) SpUtils.get(OutSideTaskActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ysid",ysid);
        map.put("rwzt",rwzt);
        OkHttpUtils.getNetWork(OutSideTaskActivity.this, Constant.baseurl+"online/wqrw/wqrwList",map,handler);
        //idnumber
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    OutSideBean bean=gson.fromJson(s,OutSideBean.class);
                    outSideTaskAdapter = new OutSideTaskAdapter(OutSideTaskActivity.this,bean.getData());
                    recyclerview_out_side_task.setAdapter(outSideTaskAdapter);
                    outSideTaskAdapter.setOnItemClickListener(new OutSideTaskAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int postion) {
                            if(bean.getData().get(postion).getRwzt()==1){  //未完成
                                Intent intent=new Intent(OutSideTaskActivity.this,TaskReportingActivity.class);
                                intent.putExtra("outid",bean.getData().get(postion).getId());
                                startActivity(intent);
                            }else{  //已完成
                                Intent intent=new Intent(OutSideTaskActivity.this,TaskDetailsActivity.class);
                                intent.putExtra("outid",bean.getData().get(postion).getId());
                                startActivity(intent);
                            }
                        }
                    });
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(OutSideTaskActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(OutSideTaskActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(OutSideTaskActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public void initData() {
        recyclerview_out_side_task.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerview_out_side_task.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                recyclerview_out_side_task.refreshComplete();
            }

            @Override
            public void onLoadMore() {

                recyclerview_out_side_task.loadMoreComplete();
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            //返回键
            case R.id.re_back:

                finish();
                break;
            case R.id.re_pending:  //待处理
                tv_pending.setTextColor(Color.parseColor("#0078FF"));
                tv_line_pending.setVisibility(View.VISIBLE);
                tv_processed.setTextColor(Color.parseColor("#000000"));
                tv_line_processed.setVisibility(View.INVISIBLE);
                getNetWork(1);
                break;
            case R.id.re_processed:  //已处理
                tv_pending.setTextColor(Color.parseColor("#000000"));
                tv_line_pending.setVisibility(View.INVISIBLE);
                tv_processed.setTextColor(Color.parseColor("#0078FF"));
                tv_line_processed.setVisibility(View.VISIBLE);
                getNetWork(2);
                break;
        }
    }
}
